﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NavKet.Models;
using System.IO;

namespace NavKet.Controllers
{
    public class MallAdminController : Controller
    {
        //
        // GET: /MallAdmin/
        MallEntities1 navKetEntity = new MallEntities1();
        public ActionResult Index(int param)
        {
            Mall mall = navKetEntity.SelectMallByMallId(param).SingleOrDefault();
            ViewBag.qudrates = navKetEntity.SelectAllQuadrates();
            ViewBag.MallPhones = navKetEntity.SelectMallPhonesByMallId(param);
            ViewBag.Mallnews = navKetEntity.SelectMallNewsByMallId(param);
            ViewBag.MallImages = navKetEntity.SelectMallImagesByMallId(param);
            return View(mall);
        }
        [HttpPost]
        public ActionResult Index(int param, FormCollection form)
        {
            string name = form["txtMallName"];
            string street=form["txtStreetName"];
            int quadrateId = Convert.ToInt32(form["quadrate"]);
            navKetEntity.UpdateMall(param,name,null,street,quadrateId);

            #region upload Shop imgs
            string imageName;
            var posted = Request.Files["uploader"];
            imageName = Path.GetFileName(posted.FileName);
            if (!string.IsNullOrEmpty(posted.FileName))
            {
                string path = "/Content/pics/" + imageName;
                posted.SaveAs(Server.MapPath(path));
                //insert
                navKetEntity.InsertMallImage(param, imageName);
            }
            #endregion

            return RedirectToAction("/Index/"+param);
        }

        #region Mall Phone
        public ActionResult DeletMallPhone(int param)
        {
            int mallId = Convert.ToInt32(Request.QueryString["mallId"]);
            #region delete
            navKetEntity.DeleteMallPhone(param);
            #endregion
            return RedirectToAction("/index/" + mallId);
        }
        public ActionResult EditMallPhone(int param)
        {
            //services
            MallPhone MallPhone = navKetEntity.SelectMallPhone(param).SingleOrDefault();
            return View(MallPhone);
        }
        [HttpPost]
        public ActionResult EditMallPhone(int param, FormCollection form)
        {
            int mallId = Convert.ToInt32(Request.QueryString["mallId"]);
            string number = form["txtPhoneNumber"];
            string Type = form["PhoneType"];
            navKetEntity.UpdateMallPhone(param, number, Type);

            return RedirectToAction("/index/" + mallId);
        }

        public ActionResult AddMallPhone()
        {
            int mallId = Convert.ToInt32(Request.QueryString["mallId"]);
            return View(mallId);
        }
        [HttpPost]
        public ActionResult AddMallPhone(FormCollection form)
        {
            int mallId = Convert.ToInt32(Request.QueryString["mallId"]);
            string number = form["txtPhoneNumber"];
            string Type = form["PhoneType"];
            navKetEntity.InsertMallPhone(mallId, number, Type);
            return RedirectToAction("/index/" + mallId);
        }
        #endregion

        #region shopNews
        public ActionResult DeleteMallNews(int param)
        {
            int mallId = Convert.ToInt32(Request.QueryString["mallId"]);
            #region delete
            //delete
            navKetEntity.DeleteMallNews(param);
            #endregion
            return RedirectToAction("/index/" + mallId);
        }
        public ActionResult EditMallNews(int param)
        {

            MallNew mallNews = navKetEntity.SelectMallNews(param).SingleOrDefault();
            return View(mallNews);
        }
        [HttpPost]

        public ActionResult EditMallNews(int param, FormCollection form)
        {
            int mallId = Convert.ToInt32(Request.QueryString["mallId"]);
            #region edit Mall news
            string Title = form["txtTitle"].ToString();
            string Content = form["txtNewsContent"].ToString();
            navKetEntity.UpdateMallNews(param, Title, Content);
            #endregion
            return RedirectToAction("/index/" + mallId);
        }
        public ActionResult AddMallNews()
        {
            int mallId = Convert.ToInt32(Request.QueryString["mallId"]);
            return View(mallId);
        }
        [HttpPost]
        public ActionResult AddMallNews(FormCollection form)
        {
            int mallId = Convert.ToInt32(Request.QueryString["mallId"]);
            #region Add Mall news
            string Title = form["txtTitle"].ToString();
            string Content = form["txtNewsContent"].ToString();
            navKetEntity.InsertMallNews(Title, Content, DateTime.Now, mallId);
            #endregion
            return RedirectToAction("/index/" + mallId);
        }
        #endregion

        #region MallImgs
        public ActionResult DeleteShopImage(string param)
        {
            int mallId = Convert.ToInt32(Request.QueryString["mallId"]);
            int shopId = Convert.ToInt32(Session["shopId"]);
            #region delete
            //delete
            navKetEntity.DeleteMallImage(mallId, param);
            #endregion
            return RedirectToAction("/index/" + mallId);
        }

        #endregion
        void test()
        {
          

          
            #region floor
            // insert update
        
            //delete
            navKetEntity.DeleteFloor(null);
            #endregion
        }
    }
}
